<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级" prop="stage">
        <el-select v-model="queryParams.classesId" filterable placeholder="请选择">
          <el-option
            v-for="item in classesList"
            :key="item.id"
            :label="item.className"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="第几阶段" prop="stage">
        <el-select v-model="queryParams.stage" placeholder="请选择第几阶段" clearable>
          <el-option
            v-for="dict in dict.type.sys_stage"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="第几周" prop="week">
        <el-select v-model="queryParams.week" placeholder="请选择第几周" clearable>
          <el-option
            v-for="dict in dict.type.ya_week"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teaching:examrecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teaching:examrecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teaching:examrecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teaching:examrecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="examrecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="班级" align="center" prop="classesName" />
      <el-table-column label="第几阶段" align="center" prop="stage">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_stage" :value="scope.row.stage"/>
        </template>
      </el-table-column>
      <el-table-column label="第几周" align="center" prop="week">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_week" :value="scope.row.week"/>
        </template>
      </el-table-column>
      <el-table-column label="机试平均分" align="center" prop="avgComputerScore" />
      <el-table-column label="笔试平均分" align="center" prop="avgWrittenScore" />
      <el-table-column label="创建日期" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teaching:examrecord:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teaching:examrecord:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生成绩对话框 -->
    <el-dialog :title="title" :visible.sync="open" fullscreen width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班级" prop="classesId">
          <el-select v-model="form.classesId" :disabled="form.id" @change="getExamDetails" filterable placeholder="请选择">
            <el-option
              v-for="item in classesList"
              :key="item.id"
              :label="item.className"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="第几阶段" prop="stage" >
          <el-select v-model="form.stage" :disabled="form.id" @change="queryLastWeek" placeholder="请选择第几阶段">
            <el-option
              v-for="dict in dict.type.sys_stage"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="第几周" prop="week">
          <el-select v-model="form.week" :disabled="form.id" placeholder="请选择第几周">
            <el-option
              v-for="dict in weekList" :disabled="dict.disabled"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考试类型"  prop="examType" >
          <el-select v-model="form.examType"  placeholder="请选择考试类型">
            <el-option
              v-for="dict in dict.type.ya_exam_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider></el-divider>
        <el-form-item v-for="detail in form.examDetails" :label="detail.student"  >
          <el-col :span="3" v-show="form.examType==2 || form.examType==3">
            机试: <el-input  maxlength="3"
                             type="text" v-model.number="detail.computerScore" style="width: 50%" autocomplete="off"/><!--v-model="form.examDetails[i].student"-->
          </el-col>
          <el-col :span="3" v-show="form.examType==1 || form.examType==3">
            笔试: <el-input  maxlength="3"
                             type="text" v-model.number="detail.writtenScore" style="width: 50%" autocomplete="off"/><!--v-model="form.examDetails[i].student"-->
          </el-col>
          <el-col :span="3" >
            感受:
            <!--<el-input type="textarea" v-model="detail.feeling" style="width: 50%" />&lt;!&ndash;v-model="form.examDetails[i].student"&ndash;&gt;-->
            <el-select v-model="detail.feeling"  placeholder="请选择感受" style="width: 50%">
              <el-option
                v-for="dict in dict.type.ya_feeling"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-col>
          <el-col :span="5" >
            问题:
            <el-input type="textarea" :rows="1" autosize v-model="detail.problem" style="width: 80%" /><!--v-model="form.examDetails[i].student"-->
            <!--<el-input type="textarea" :rows="10" v-show="showProblemTextArea && detail.userId == editStuId" autofocus :id="'problem'+detail.userId"  @blur="editStuId=0" v-model="detail.problem" style="width: 50%" />-->
          </el-col>
          <el-col :span="5" >
            措施: <el-input type="textarea" :rows="1" autosize v-model="detail.measure" style="width: 80%" /><!--v-model="form.examDetails[i].student"-->
          </el-col>
          <el-col :span="5" >
            备注: <el-input type="textarea" :rows="1" autosize v-model="detail.remark" style="width: 80%" /><!--v-model="form.examDetails[i].student"-->
          </el-col>
        </el-form-item>
        <el-divider></el-divider>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryLastWeek,listExamdetails,listExamrecord, getExamrecord, delExamrecord, addExamrecord, updateExamrecord } from "@/api/teaching/examrecord";
import {listClasses} from "@/api/teaching/classes";

export default {
  name: "Examrecord",
  dicts: ['sys_stage', 'ya_week','ya_exam_type','ya_feeling'],
  data() {
    return {
      editStuId:0,
      showProblemTextArea:false,
      //查询上次测试周数
      lastWeek:0,
      classesList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生成绩表格数据
      examrecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createTime: null,
        stage: null,
        week: null,
        classesId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建日期不能为空", trigger: "blur" }
        ],
        stage: [
          { required: true, message: "第几阶段不能为空", trigger: "change" }
        ],
        week: [
          { required: true, message: "第几周不能为空", trigger: "change" }
        ],
        classesId: [
          { required: true, message: "班级不能为空", trigger: "change" }
        ],
        examType: [
          { required: true, message: "测试类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getClassesList();
  },
  computed:{
    weekList(){
      let result = this.dict.type.ya_week;
      for (let i = 0; i < result.length; i++) {
        if (parseInt(result[i].value) <= this.lastWeek) {
          result[i].disabled = true;
        }else{
          result[i].disabled = false;
        }
      }
      return result;
    }
  },
  /*  watch:{
      form:{
        deep:true,
        handler(nVal){

        }
      }
    },*/
  methods: {
    checkLastweek(week) {
      console.log(this.lastWeek , week);
      return this.lastWeek > parseInt(week);
    },
    queryLastWeek(){
      queryLastWeek(this.form).then(response => {
        this.lastWeek = response.data;
      });
    },
    getExamDetails(){
      listExamdetails(this.form.classesId).then(response => {
        this.form.examDetails = response.rows;
      });
    },
    /** 查询班级列表 */
    getClassesList() {
      listClasses({}).then(response => {
        this.classesList = response.rows;
      });
    },
    /** 查询学生成绩列表 */
    getList() {
      this.loading = true;
      listExamrecord(this.queryParams).then(response => {
        this.examrecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        stage: null,
        week: null,
        classesId: null,
        examDetails:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生成绩";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getExamrecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生成绩";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExamrecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExamrecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生成绩编号为"' + ids + '"的数据项？').then(function() {
        return delExamrecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teaching/examrecord/export', {
        ...this.queryParams
      }, `examrecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
